package com.example.financecollegeorderdish.ui

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import com.bumptech.glide.Glide
import com.example.financecollegeorderdish.R
import com.example.financecollegeorderdish.databinding.ActivityEvaluateBinding
import com.example.financecollegeorderdish.db.bean.DishBean
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.EmptyViewModel
import com.wuhao.lib_base.mvvm.v.BaseActivity
import com.wuhao.lib_common.utils.LoggerHelper
import java.lang.reflect.Type
import java.util.Random

class EvaluateActivity : BaseActivity<ActivityEvaluateBinding, EmptyViewModel>() {
    override val mViewModel: EmptyViewModel
        get() = EmptyViewModel()
    val scoreType: Typeface by lazy {
        Typeface.createFromAsset(
            assets,
            "fonts/GaoDuanHeiXiuDing151105-1.ttf"
        )
    }//旁门正道标题
    val pmzdbt: Typeface by lazy {
        Typeface.createFromAsset(
            assets,
            "fonts/庞门正道标题体免费版.ttf"
        )
    }//旁门正道标题

    /**
     * 创建 [ViewBinding] 实例
     */
    override fun createVB(): ActivityEvaluateBinding =
        ActivityEvaluateBinding.inflate(layoutInflater)

    /**
     * 2-订阅 [LiveData]
     */
    override fun initObserve() {
    }

    /**
     * 3-用于在页面创建时进行请求接口
     */
    override fun initRequestData() {
    }

    var evType = 1
    lateinit var dishList: List<DishBean>

    /**
     * 1-初始化 View
     */
    override fun ActivityEvaluateBinding.initView() {
        val dishes: String = intent.getStringExtra("dishes")!!//获取用户姓名
        val listType: Type = object : TypeToken<List<DishBean>>() {}.type
        dishList = Gson().fromJson<List<DishBean>>(dishes, listType)

        if (dishList.isNotEmpty() && dishList.size == 4) {
            for (dishBean in dishList) {
                if (dishBean.name == "蜜汁黑猪排") {// || dishBean.name == "宝塔扣肉" || dishBean.name == "青酱椒麻鸡" || dishBean.name == "鱼子酱大鹅脯"
                    evType = 1
                    break
                } else if (dishBean.name == "西芹百合") {//dishBean.name == "爽脆丝瓜尖" || dishBean.name == "桂花山药糕" || dishBean.name == "春笋腌肉炖豆腐" ||
                    evType = 2
                    break
                } else if (dishBean.name == "白灼秋葵") {// || dishBean.name == "鲜花椒生爆小牛肉粒" || dishBean.name == "爽脆丝瓜尖" || dishBean.name == "桂花山药糕"
                    evType = 3
                }

            }
        } else {
            evType = 1
        }

        mBinding.score.typeface = scoreType
        mBinding.score.typeface = pmzdbt

        LoggerHelper.e("${randomFloatInRange(0f, 1f)}")
        setValue()
    }

    fun randomFloatInRange(min: Float, max: Float): Float {
        val randomValue = min + Random().nextFloat() * (max - min)
        return "%.2f".format(randomValue).toFloat()
    }

    fun setValue() {
        when (evType) {
            1 -> {
                mBinding.score.text = "76.5"
                mBinding.tvSuggest.text = getString(R.string.suggest_1)
            }

            2 -> {
                mBinding.score.text = "80.6"
                mBinding.tvSuggest.text = getString(R.string.suggest_2)
            }

            3 -> {
                mBinding.score.text = "95.5"
                mBinding.tvSuggest.text = getString(R.string.suggest_3)
            }

            else -> {}
        }
        if (dishList.isNotEmpty() && dishList.size == 4) {
            for ((index, dishBean) in dishList.withIndex()) {
                when (index) {
                    0 -> {
                        mBinding.tvDish1.text = "${dishBean.name}    ￥${dishBean.price}"
                        Glide.with(this@EvaluateActivity)
                            .load(dishBean.imagePath)
                            .into(mBinding.ivDish1)
                    }

                    1 -> {
                        mBinding.tvDish2.text = "${dishBean.name}    ￥${dishBean.price}"
                        Glide.with(this@EvaluateActivity)
                            .load(dishBean.imagePath)
                            .into(mBinding.ivDish2)
                    }

                    2 -> {
                        mBinding.tvDish3.text = "${dishBean.name}    ￥${dishBean.price}"
                        Glide.with(this@EvaluateActivity)
                            .load(dishBean.imagePath)
                            .into(mBinding.ivDish3)
                    }

                    3 -> {
                        mBinding.tvDish4.text = "${dishBean.name}    ￥${dishBean.price}"
                        Glide.with(this@EvaluateActivity)
                            .load(dishBean.imagePath)
                            .into(mBinding.ivDish4)
                    }

                    else -> {}
                }
            }
        }


        mBinding.heat.text = "${randomFloatInRange(1f, 2f)}千焦"
        mBinding.protein.text = "${randomFloatInRange(10f, 20f)}克"
        mBinding.calorie.text = "${randomFloatInRange(200f, 300f)}千卡"
        mBinding.dietaryfiber.text = "${randomFloatInRange(0f, 1f)}克"

        mBinding.fat.text = "${randomFloatInRange(15f, 30f)}克"
        mBinding.purine.text = "${randomFloatInRange(100f, 800f)}毫克"
        mBinding.sugar.text = "${randomFloatInRange(0f, 10f)}克"

        mBinding.vitaminb1.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb2.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb3.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb5.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb6.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb7.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb9.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminb12.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitaminc.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitamina.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitamind.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitamine.text = "${randomFloatInRange(0f, 1f)}毫克"
        mBinding.vitamink.text = "${randomFloatInRange(0f, 1f)}毫克"

        mBinding.iron.text = setFirstTwoCharsColor("铁:${randomFloatInRange(1f, 2f)}毫克")//铁
        mBinding.manganese.text = setFirstTwoCharsColor("锰:${randomFloatInRange(0f, 0.5f)}毫克")//锰
        mBinding.zinc.text = setFirstTwoCharsColor("锌:${randomFloatInRange(1f, 5f)}毫克")//锌
        mBinding.copper.text = setFirstTwoCharsColor("铜:${randomFloatInRange(0f, 0.5f)}毫克")//铜
        mBinding.potassium.text =
            setFirstTwoCharsColor("钾:${randomFloatInRange(100f, 500f)}毫克")//钾
        mBinding.phosphorus.text =
            setFirstTwoCharsColor("磷:${randomFloatInRange(90f, 200f)}毫克")//磷
        mBinding.sodium.text = setFirstTwoCharsColor("钠:${randomFloatInRange(40f, 100f)}毫克")//钠
        mBinding.calcium.text = setFirstTwoCharsColor("钙:${randomFloatInRange(0f, 1f)}毫克")//钙
        mBinding.selenium.text = setFirstTwoCharsColor("硒:${randomFloatInRange(10f, 20f)}毫克")//硒
        mBinding.magnesium.text = setFirstTwoCharsColor("镁:${randomFloatInRange(10f, 20f)}毫克")//镁
    }

    fun setFirstTwoCharsColor(text: String): SpannableString {
        val spannable = SpannableString(text)
        spannable.setSpan(
            ForegroundColorSpan(getColor(R.color.ev_tv_color)),
            0,
            2,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return spannable
    }

    fun start(
        context: Context,
        dishes: String
    ) {
        val starter =
            Intent(context, EvaluateActivity::class.java)
        starter.putExtra("dishes", dishes)
        context.startActivity(starter)
    }
}